using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvaloniaMaid2022.Helpers
{
    public static class NamespaceHelper
    {

        private static string[] SpecialNames = Array.Empty<string>();

        static NamespaceHelper()
        {
            SpecialNames = new string[] { 
                "viewmodel", "viewmodels", 
                "view", "views", 
                "models", "model", 
                "entities", "entity", 
                "control", "controls", 
                "converter", "converters", 
                "helper", "helpers", 
                "util", "utils",
                "style", "styles",
                "resource", "resources"
            }.Distinct().ToArray();
        }

        /// <summary>
        /// 获得命名空间的短名。
        /// </summary>
        /// <param name="namespaceName"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static string GetNamespaceShortName(string namespaceName)
        {
            if (string.IsNullOrWhiteSpace(namespaceName)) throw new ArgumentNullException("命名空间为空");
            var prefixIndex = namespaceName.IndexOf('.');
            if (prefixIndex == -1) return "app"; // 找不到子内容时

            var specialNames = SpecialNames;

            var stack = new Stack<string>();

            var parts = namespaceName.Split('.');

            foreach (var part in parts.Skip(1))
            {
                var lowerPart = part.ToLower();
                if (specialNames.Contains(lowerPart)) stack.Clear();
                stack.Push(lowerPart);
            }

            return string.Join("-", stack.ToArray());

        }
    }
}
